package com.main.hotel.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.alipay.api.request.AlipayTradeRefundRequest;
import com.main.hotel.config.AlipayConfig;
import com.main.hotel.entity.AlipayBean;
import com.main.hotel.util.OrderUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@Service
// HttpServletRequest request, HttpServletResponse response
public class AliPayServiceImpl {
    public String alipay(String outTradeNo, String subject, String totalAmount, String body, String notifyUrl,int foo) {
      AlipayBean alipayBean = new AlipayBean();
      alipayBean.setOut_trade_no(outTradeNo);
      alipayBean.setSubject(subject);
      alipayBean.setTotal_amount(totalAmount);
      alipayBean.setBody(body);
      // 1、获得初始化的AlipayClient
      AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.GATEWAY_URL, AlipayConfig.APP_ID, AlipayConfig.PRIVARY_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.PUBLIC_KEY, AlipayConfig.SIGN_TYPE);
      // 2、设置请求参数
      AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
      // 页面跳转同步通知页面路径
      alipayRequest.setReturnUrl(AlipayConfig.RETURN_URL+"?foo="+foo);
      // 服务器异步通知页面路径
      alipayRequest.setNotifyUrl(notifyUrl);
      // 封装参数
      alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
      String form = "";
      try {
        // 调用SDK生成表单
        // 发起支付且成功以后会返回一个支付宝收银台页面的地址,按照官方demo直接到页面即可
        form = alipayClient.pageExecute(alipayRequest).getBody();
      } catch (AlipayApiException e) {
        // TODO Auto-generated catch block
        }
      // 返回付款信息
      return form;

    }

  public String refund(String order_id,String cost) throws IOException, AlipayApiException
  {
    //response.setContentType("text/html;charset=utf-8");

 // PrintWriter out = response.getWriter();    //获得初始化的AlipayClientAlipayClient
    AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.GATEWAY_URL, AlipayConfig.APP_ID,AlipayConfig.PRIVARY_KEY, AlipayConfig.FORMAT,AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGN_TYPE);    //设置请求参数
     AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();    //商户订单号，必填
     String out_trade_no = new String(order_id);    //需要退款的金额，该金额不能大于订单金额，必填

     String refund_amount = new String(cost);    //标识一次退款请求，同一笔交易多次退款需要保证唯一，如需部分退款，则此参数必传
     String out_request_no = new String(OrderUtil.getOrderId());
     alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","+ "\"refund_amount\":\""+ refund_amount +"\","+ "\"out_request_no\":\""+ out_request_no +"\"}");
    String result = alipayClient.execute(alipayRequest).getBody();
    return "success";
}
}

